package com.nuoniu.sibanyun.mapper.erp;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nuoniu.sibanyun.entity.erp.ErpProject;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * <p>
 *  项目Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-10-12
 */
public interface ErpProjectMapper extends BaseMapper<ErpProject> {

    /**
     * 校验名称是否重复
     * @param companyId
     * @param accountId
     * @param name
     * @param oldId
     * @return
     */
    boolean isHaveName(Integer companyId,Integer accountId,String name,Long oldId);

    /**
     * 校验 CODE 是否重复
     * @param companyId
     * @param accountId
     * @param code
     * @param oldId
     * @return
     */
    boolean isHaveCode(Integer companyId,Integer accountId,String code,Long oldId);

    /**
     * 修改上级信息
     * @author 小啊刚
     * @date 2020-09-25
     */
    @Update("update prj_project set parent_ids=REPLACE(parent_ids, #{parentIds}, #{newParentIds}), tree_names=REPLACE(tree_names, #{treeNames}, #{newTreeNames}) " +
            "where  FIND_IN_SET(#{id}, parent_ids ) or id = #{id}")
    void updateChildrenParentIds(Long id, String parentIds, String treeNames, String newParentIds, String newTreeNames);

    /**
     * 获取子级数量
     * @author 小啊刚
     * @date 2021-10-19
     */
    @Select("select count(1) from prj_project where parent_id=#{parentId}")
    Integer getCountByParentId(Integer parentId);

    /**
     * 获取最新编码
     * @author 小啊刚
     * @date 2021-10-23
     */
    @Select("select code from prj_project where company_id=#{companyId} and date_format(now(), #{dateStr})=date_format(create_date, #{dateStr}) order by create_date desc limit 1")
    String getCode(Integer companyId, String dateStr);
}
